<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
<!--                    <a-col :span="24">-->
<!--                      <a-form-model-item  label="subjectId" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subjectId">-->
<!--                        <a-input-number readonly v-model="model.subjectId" placeholder="请输入subjectId" style="width: 100%" />-->
<!--                      </a-form-model-item>-->
<!--                    </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subjectCode">
              <a-input v-model="model.subjectCode" placeholder="请输入编码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="科目名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subjectName">
              <a-input v-model="model.subjectName" placeholder="请输入科目名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="简称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subjectAbbreviation">
              <a-input v-model="model.subjectAbbreviation" placeholder="请输入简称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subjectTtype">
              <j-dict-select-tag @change="handleChanegObjType" type="list" v-model="model.subjectTtype" dictCode="subject_ttype" placeholder="请输入类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="父级科目/专业" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subjectParentCode">
<!--              <a-input v-model="model.subjectParentCode" placeholder="请输入父级编码"></a-input>-->
              <j-dict-select-tag v-model="model.subjectParentCode" dictCode="cou_subject,subject_name,id" placeholder="请输入父级" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="场地要求" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subjectDemand">
              <a-input v-model="model.subjectDemand" placeholder="请输入场地要求"></a-input>
            </a-form-model-item>
          </a-col>
          <!--          <a-col :span="24">-->
          <!--            <a-form-model-item label="学校编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="tenantId">-->
          <!--              <a-input v-model="model.tenantId" placeholder="请输入学校编码"  ></a-input>-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="是否可用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subjectIzOpen">
<!--              <a-input-number v-model="model.subjectIzOpen" placeholder="请输入是否可用" style="width: 100%" />-->
              <j-dict-select-tag @change="handleChanegObjType" type="list" v-model="model.subjectIzOpen" dictCode="is_open_type" placeholder="请输入是否可用" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subjectStatus">
<!--              <a-input-number v-model="model.subjectStatus" placeholder="请输入状态" style="width: 100%" />-->
              <j-dict-select-tag @change="handleChanegObjType" type="list" v-model="model.subjectStatus" dictCode="subject_status" placeholder="请输入状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subjectOrder">
              <a-input-number v-model="model.subjectOrder" placeholder="请输入排序" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'CouSubjectForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        subjectCode: [
          { required: true, message: '请输入编码!' }
        ],
        subjectName: [
          { required: true, message: '请输入科目名称!' }
        ],
        // subjectParentCode: [
        //   { required: true, message: '请输入父级编码!' }
        // ],
      },
      url: {
        add: '/subject/couSubject/add',
        edit: '/subject/couSubject/edit',
        queryById: '/subject/couSubject/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      if (localStorage.length > 0) {
        let localData = JSON.parse(localStorage.getItem('pro__Login_Userinfo'))
        let tenantId = localData.value.tenantId
        this.model.tenantId = tenantId
      }
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    }
  }
}
</script>